home *** CD-ROM | disk | FTP | other *** search
- /* Print names of .EXE files in current directory of current drive. */
-
- #include <stdio.h>
- #include <dir.h>
- #include <process.h>
- #include <dos.h>
-
- int main(void)
- {
- struct ffblk filedetails; // Create structure in which findfirst() and
- // findnext() can put details of files they
- // find. The tag ffblk is defined in DIR.H
-
- // PATHNAME ADDRESS OF STRUCTURE OF TYPE FFBLK IN
- // | | WHICH TO PUT DETAILS OF FOUND FILE
- // | |
- // | | FILE ATTRIBUTE
- // | | |
- // -------- ------------ ---
- if( findfirst("*.EXE", &filedetails, 0 ) == -1 )
- {
- printf("Nothing found!\n");
- exit(0); // If no match (findfirst() returned -1) then exit program
- }
-
- do { // If program gets this far, there must be a name to print
- printf("%s\n", filedetails.ff_name);
- } while( findnext(&filedetails) == 0 ); // Now only loop if there is
- // another filename. Return
- // value of 0 indicates
- // success. See note below.
- exit(-1); // Flags success as DOS ERRORLEVEL = -1
- }
-
- /* NOTES
- 1: When you want to say 'while value is zero' you can alternatively write
- 'while not value':
-
- while ( ! value )
-
- ! means 'not'. Remember that a non-zero value equates as true, while zero
- equates as false. A loop of 'while(value)' would only continue as long as
- value isn't zero. Not (!) flips the result of a test to the opposite of its
- truth or falsehood, so 'while( ! value )' is true as long as value is zero.
- The loop condition above could therefore be written:
-
- while( ! findnext(&filedetails) )
-
- 2: Load DIR.H to see the template that the structure tag ffblk defines.
- Alternatively, put the cursor on ffblk and press Ctrl+Shift+F1. In it
- you will find a char array called ff_name[] into which the name of the found
- file is placed. Filedetails.ff_name is therefore a pointer to this string
- which the %s(tring) in printf() picks up.
-
- 3: The use of a do...while loop enables us to print the filename even if it
- is the only one - the first printf() occurs before the loop condition has been
- tested. If an ordinary while loop been used, a duplicate printf() line would
- have been needed before the start of the loop. Otherwise, in a case where
- there was only one file, the loop test would fail and its name wouldn't be
- printed.
-
- 4: File attribute in findfirst() - more info in CPROG15.CPP.
- */
-